//
//  BMASong.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/1.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BMASong.h"
#import "NSString+CZExtend.h"
#import "NSString+AES.h"

@implementation BMASong

/**
 * 歌曲基本信息
 * @param songid	歌曲id
 * @return
 */
- (NSString *) songBaseInfo:(NSString *)songid{
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.baseInfos"];
    [sb appendFormat:@"&song_id=%@",songid];
    return sb;
}

/**
 * 编辑推荐歌曲
 * @param num	数量
 * @return
 */
- (NSString *) recommendSong:(int)num{
    
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.getEditorRecommend"];
    [sb appendFormat:@"&num=%d",num];
    return sb;
}

/**
 * 歌曲信息和下载地址
 * @param songid
 * @return
 */
- (NSString *) songInfo:(NSString *)songid{
    
    NSString *INPUT = @"2012171402992850"; //用INPUT生成了MD5密钥
    NSString *md5Key=[INPUT stringFromMD5];
    
    NSString *ascKey=@"2012061402992850";
    
    NSTimeInterval  ts=[[NSDate date] timeIntervalSince1970];
    
    NSString *str=[NSString stringWithFormat:@"songid=%@&ts=%f",songid,ts];
    NSString *e=[str AES256_Encrypt:md5Key]; //str使用encrpty加密
    
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.getInfos"];
    [sb appendFormat:@"&%@",str];
    [sb appendFormat:@"&e=%@",e];
    return sb;
}

/**
 * 歌曲伴奏信息
 * @param songid
 * @return
 */
- (NSString *) accompanyInfo:(NSString *)songid{
    
    NSTimeInterval  ts=[[NSDate date] timeIntervalSince1970];
    
    NSString *str=[NSString stringWithFormat:@"song_id=%@&ts=%f",songid,ts];
    NSString *e=[str stringFromMD5]; //str使用encrpty加密
    
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.learn.down"];
    [sb appendFormat:@"&%@",str];
    [sb appendFormat:@"&e=%@",e];
    return sb;
}

/**
 * 相似歌曲 （推荐歌曲）
 * @param songid
 * @return
 */
- (NSString *) recommendSongList:(NSString *)songid num:(int)num{
    
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.getRecommandSongList"];
    [sb appendFormat:@"&song_id=%@",songid];
    [sb appendFormat:@"&num=%d",num];
    return sb;
}

/**
 歌词
 @param songid 歌曲id
 @return url
 */
- (NSString *) lyric:(NSString *)songid{
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.lry"];
    [sb appendFormat:@"&query=%@",songid];
    return sb;
}

/**
 下载
 @param songid 歌曲id
 @return url
 */
- (NSString *) downloadSong:(NSString *)songid{
    
    /**
     
     参数：	songid = 877578//歌曲id
     
     bit = 24, 64, 128, 192, 256, 320 ,flac//码率
     
     _t = 1430215999,, //时间戳
     
     **/
    
    NSTimeInterval  ts=[[NSDate date] timeIntervalSince1970];
    
    NSMutableString *sb = [[NSMutableString alloc] initWithString:[self baseURLString]];
    [sb appendString:@"&method=baidu.ting.song.downWeb"];
    [sb appendFormat:@"&songid=%@",songid];
    [sb appendString:@"&bit=24"];
    [sb appendFormat:@"&_t=%f",ts];
    return sb;
}

@end
